var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const routes = require('./routes')
const cors = require('cors')
const errorHandle = require('./middlewarn/error-handle')

require('./model')

var app = express();


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// 输出日志
app.use(logger('dev'));
// -----end ------

//解析请求体
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// -----end ------

//配置跨域请求
app.use(cors())
//------end -----

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use("/api",routes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// 统一处理错误
app.use(errorHandle());

module.exports = app;
